//
//  OpMineController.m
//  LoveFreshBeeReplica
//
//  Created by skywalker on 16/12/30.
//  Copyright © 2016年 斯芬克斯. All rights reserved.
//

#import "OpMineController.h"
#import "MineView.h"
#import "NetworkTool.h"
#import "OpAddRessView.h"
#import "OpShopView.h"
#import "OpHelpView.h"
#import "OpIdeaView.h"
#import "MineTableViewCell.h"
#import "MyCouponViewController.h"
#import "MyMessageViewController.h"
#import "MyAddressTableViewCell.h"
#import "SettingViewController.h"
@interface OpMineController ()

@property(nonatomic,strong) NSMutableArray *arr;

@property(nonatomic,weak)UIButton *btn;

@end
static NSString *mineCellID = @"mineCellID";
static NSString *otherCellID = @"otherCellID";
@implementation OpMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 150)];
    UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
    [headerView addSubview:iv];
    UILabel *versonLabel = [[UILabel alloc]init];
    versonLabel.text = @"18635629040";
    [headerView addSubview:versonLabel];
    
    UIButton *setBtn = [[UIButton alloc]init];
    [setBtn setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
    
    [headerView addSubview:setBtn];
    
    [setBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10);
        make.centerX.equalTo(headerView);
    }];
    [versonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headerView).offset(-10);
        make.centerX.equalTo(headerView);
    }];
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10);
        make.right.equalTo(headerView).offset(-10);
    }];
    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
    self.tableView.tableHeaderView = headerView;

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:otherCellID];
    self.tableView.separatorInset = UIEdgeInsetsZero;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:mineCellID];

}
- (void)loadButtonData{

    NSArray *btnArr = self.mineView.arrM;
    NSLog(@"%@",btnArr);
    for (int i = 0; i < btnArr.count; i++) {
        UIButton *btn = btnArr[i];
        [btn addTarget:self action:@selector(btnTouchUpWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}


-(void)btnTouchUpWithBtn:(UIButton *)btn
{
    switch (btn.tag) {
        case MyOrderBtnType:{
            
            break;
        }
        case MyCouponBtnType:{
            // 创建控制器
            MyCouponViewController *couponVC = [[MyCouponViewController alloc] init];
            couponVC.title = @"优惠券";
            [self.navigationController pushViewController:couponVC animated:YES];
            break;
        }
        case MyMessageType:{
            MyMessageViewController *messageVC = [[MyMessageViewController alloc] init];
            [self.navigationController pushViewController:messageVC animated:YES];
            break;
        }
    }

    self.tableView.tableFooterView = [UIView new];
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return 1;
    }else{
        return 2;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        MineTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:mineCellID forIndexPath:indexPath];
        self.mineView = cell.mineView;
        [self loadButtonData];
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:otherCellID forIndexPath:indexPath];
        if(indexPath.section == 1 && indexPath.row == 0)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_address_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"我的收获地址";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 1 && indexPath.row == 1)
        {
            UIImage *image = [UIImage imageNamed:@"icon_mystore"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"我的店铺";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 2)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_share_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"把爱鲜蜂分享给别人";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 3 && indexPath.row == 0)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_serviceonline_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"客服帮助";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 3 && indexPath.row == 1)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_feedback_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"意见反馈";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return 80;
    }
    return 40;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1 && indexPath.row == 0)
    {
        OpAddRessView *vc = [[OpAddRessView alloc]init];
        vc.title = @"收货地址";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 1 && indexPath.row == 1)
    {
        OpShopView *vc = [[OpShopView alloc]init];
        vc.title = @"我的店铺";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 3 && indexPath.row == 0)
    {
        OpHelpView *vc = [[OpHelpView alloc]init];
        vc.title = @"客服帮助";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 3 && indexPath.row == 1)
    {
        OpIdeaView *vc = [[OpIdeaView alloc]init];
        vc.title = @"意见反馈";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 2)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
        UIAlertAction *QXAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *WXAction = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *WBAction = [UIAlertAction actionWithTitle:@"微博" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *QQAction = [UIAlertAction actionWithTitle:@"QQ" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:QXAction];
        [alertController addAction:WXAction];
        [alertController addAction:WBAction];
        [alertController addAction:QQAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (void)BtnClick:(UIButton *)btn {
    

    SettingViewController * vc = [[SettingViewController alloc] init];
    vc.title = @"设置";

    [self.navigationController pushViewController:vc animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
